import VueRouter, { NavigationGuardNext } from "vue-router";
import Vue from "vue";
import prRoutes from "./purchase-receipt";
import mcRoutes from "./material-cost";
import aeoRoutes from "./account-enter-out";
import saRoutes from "./sys-admin";
import trsRoutes from "./transact-all";
import demoRoutes from "./demo";
import { Message } from "element-ui";
import { MODE } from "@/views/data";

Vue.use(VueRouter); // 还要use一下，啰嗦的一匹

export const routes = [
  {
    path: "/",
    redirect: "/login",
    // redirect: "/login",
    beforeEnter: (to: any, from: any) => {
      console.log("独享守卫", to, from);
    },
  },
  {
    path: MODE === "fin" || MODE === "finDev" ? "/cas" : "/cms",
    component: () => import("@/views/Home.vue"),
    children: [
      ...prRoutes,
      ...mcRoutes,
      ...aeoRoutes,
      ...saRoutes,
      ...trsRoutes,
      ...demoRoutes,
      { path: "/404", component: () => import("@/components/404.vue") },
    ],
  },
  {
    path: "/login",
    component: () => import("@/views/login/Login.vue"),
  },
  {
    path: "/h",
    component: () => import("@/components/Guide.vue"),
  },
  {
    path: "/:pathMatch(.*)*",
    name: "NotFound",
    component: () => import("@/components/404.vue"),
    meta: {
      title: "未找到",
      isPublic: true,
    },
  },
];
const router = new VueRouter({
  mode: "history",
  base: import.meta.env.VITE_BASE_URL,
  routes,
});
router.beforeEach((to: any, from: any, next: NavigationGuardNext<any>) => {
  // debugger;
  console.log("beforeEach", to, from, router.currentRoute.path === from.path);
  if (localStorage.getItem("token")) {
    next();
  } else if (to.path !== "/" && to.path !== "/login") {
    Message.error("请先登录！");
    next("/login");
  } else {
    next();
  }
});
export default router;
